<?php
	class Anchors extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}

    	/**
        * 函数功能介绍：返回课程列表（用于分页显示）
        * 创建人:  Yang Ningshu
        * @param id int
        * @return array
        */
        function get_list_ofpage($limit = 0, $offset = 10,$data = 0){
            $where = "1=1 and cs.school_id=".$this->user_data['school_id'];
            $order = "cs.id desc";

            if($data['status'] != 10){
                $where .= " AND cs.status = '". $data['status']."'";
            }

             if($data['type'] > 0){
                $where .= " AND cs.type = '". $data['type']."'";
            }

            if($data['cate_id'] && $data['cate_id'] > 0){
                $where .= " AND cs.cate_id = '". $data['cate_id']."'";
            }

            if($data['kemu'] && $data['kemu'] > 0){
                $where .= " AND v.subject_id = '". $data['kemu']."'";
            }

            if(!empty($data['search'])) {
                $where .= " AND v.title like '%".$data['search']."%'";
            }

            // $sql = "SELECT SQL_CALC_FOUND_ROWS v.*,(select count(0) from {$this->db->dbprefix}shopping_order_item where course_id = v.id) as sale_count,
						//  cs.fx_price,cs.commission,cs.status as course_status,cs.id as sjid,cs.is_fenxiao,cs.is_free FROM {$this->db->dbprefix}course as v
						//    JOIN {$this->db->dbprefix}course_shelves cs on cs.course_id = v.id
						// 	WHERE $where ORDER BY ".$order." LIMIT $limit, $offset";

							$sql = "SELECT SQL_CALC_FOUND_ROWS v.*,(select count(0) from {$this->db->dbprefix}shopping_order_item where course_id = v.id) as sale_count,
							 cs.fx_price,cs.commission,cs.status as course_status,cs.id as sjid,cs.is_fenxiao,cs.is_free,cate.name as cate_name
							  FROM {$this->db->dbprefix}course_shelves as cs
							   JOIN {$this->db->dbprefix}course v on v.id=cs.course_id
								 LEFT JOIN {$this->db->dbprefix}course_cate cate on cate.id = cs.cate_id
								WHERE $where ORDER BY ".$order." LIMIT $limit, $offset";

								// print_r($sql);exit;

            $result = $this->db->query($sql)->result_array();


            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();

            $this->db->trans_complete();



            return array("count" => $row_num['row_num'], "result" => $result);
        }

		/**
		 * 获取直播
		 * @param integer $limit  [description]
		 * @param integer $offset [description]
		 * @param integer $data   [description]
		 */
		function get_zhibo_list_ofpage($limit = 0, $offset = 10,$data = 0){
            $where = "1=1 and v.school_id=".$this->user_data['school_id'];
            $order = "v.id desc";

            if($data['status'] != 10){
                $where .= " AND v.status = '". $data['status']."'";
            }

             if($data['type'] > 0){
                $where .= " AND v.type = '". $data['type']."'";
            }

            if($data['cate_id'] && $data['cate_id'] > 0){
                $where .= " AND v.cate_id = '". $data['cate_id']."'";
            }

            if($data['kemu'] && $data['kemu'] > 0){
                $where .= " AND v.subject_id = '". $data['kemu']."'";
            }

            if(!empty($data['search'])) {
                $where .= " AND v.title like '%".$data['search']."%'";
            }

            if($this->is_manage != 1){
                $where .= " AND v.teacher_id = '{$this->user_data['teacher_id']}' ";
            }
            $sql = "SELECT SQL_CALC_FOUND_ROWS v.*,(select count(0) from {$this->db->dbprefix}shopping_order_item where course_id = v.id) as sale_count,
						 cs.fx_price,cs.commission,cs.status as course_status,cs.id as sjid,cs.is_fenxiao,cs.is_free FROM {$this->db->dbprefix}course as v
						  LEFT JOIN {$this->db->dbprefix}course_shelves cs on cs.course_id = v.id
							WHERE $where ORDER BY ".$order." LIMIT $limit, $offset";
							// print_r($sql);exit;
							// $sql = "SELECT SQL_CALC_FOUND_ROWS v.*,(select count(0) from {$this->db->dbprefix}shopping_order_item where course_id = v.id) as sale_count,
							//  cs.fx_price,cs.commission,cs.status as course_status,cs.id as sjid,cs.is_fenxiao,cs.is_free
							//   FROM {$this->db->dbprefix}course_shelves as cs
							//    JOIN {$this->db->dbprefix}course v on v.id=cs.course_id
							// 	WHERE $where ORDER BY ".$order." LIMIT $limit, $offset";

								// print_r($sql);exit;

            $result = $this->db->query($sql)->result_array();


            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();

            $this->db->trans_complete();



            return array("count" => $row_num['row_num'], "result" => $result);
        }



        function get_zhiboke_info($id){
            if(empty($id)) return array();
            $sql = "SELECT * FROM {$this->db->dbprefix}course where id=".$id."";
            // $sql.= " WHERE ";
            // if(is_array($id)) {
            //     $ids = implode(",",$id);
            //     $sql.= " id IN (". $ids .") ";
            // }else {
            //     $sql.= " ";
            // }

            $result = $this->db->query($sql)->row_array();
            return $result;
        }

	}
